window.LoginController = function( opt )
{
    // 插件入口ID
    this.plugName = opt.plugName || "s_simnew61";
    // 密钥用户名
    this.keyUserName = opt.keyUserName || "mingteng";
    // 密钥密码
    this.keyPassWord = opt.keyPassWord || "mingteng";
    // 用户名存储位置
    this.userNamePath = opt.userNamePath || 0;
    // mac地址存储位置
    this.macPath = opt.macPath instanceof Array ? opt.macPath : [];
    // 驱动作用域
    this._driver = null;
    // 设备地址
    this._findPort = null;
    // KEY ID 解密字符
    this._digitArray = new Array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f');
    // 循环判断是否插入key
    this._isKey = null;
    this._prevKey = null;
};

window.LoginController.prototype = {
    // KEY 解密
    _toHex : function( n )
    {
         var result = '';
         var start = true;
         for (var i = 32; i > 0;) {
            i -= 4;
            var digit = ( n >> i ) & 0xf;
            if (!start || digit != 0) {
                start = false;
                result += this._digitArray[digit];
            }
         }
         
         return ( result == '' ? '0' : result );
    },
    
    // 获取KEY ID
    getKeyID : function()
    {
        return this._toHex( this._driver.GetID_1( this.getDriverpath() )) + this._toHex( this._driver.GetID_2( this.getDriverpath() ));
    },
    /** 获取插件对象*/
    getDriver : function( callback )
    {
        var plug;

        var userAgent = window.navigator.userAgent;

        if ( userAgent.indexOf("MSIE") > 0 && !userAgent.indexOf("opera") > -1 )
        {
            plug = new ActiveXObject( "Syunew6A.s_simnew6" );
        }
        else
        {
            plug = document.getElementById( this.plugName );
        }
        try{
            plug = new ActiveXObject("Syunew6A.s_simnew6");
        }catch(e){
            plug = document.getElementById( this.plugName );
        }
         
        this._driver = plug;
        
        var isDriver = this.isInstallDriver();
        
        if( !isDriver ) typeof callback == "function" && callback.call( this );
        
        return isDriver;
    },

    // 获取缓冲区数据
    _getCachedata : function( start )
    {
        var getDriverpath = this._findPort || this.getDriverpath();

        var lastError = this._driver.YRead( start, 1, this.keyUserName, this.keyPassWord, getDriverpath );

        if( lastError !== 0 ) return null;

        var userNamelen = this._driver.GetBuf( 0 );

        return this._driver.YReadString( start + 1, userNamelen,this.keyUserName, this.keyPassWord, getDriverpath ) || null;
    },

    setCacheData : function( mac )
    {
        var getDriverpath = this._findPort || this.getDriverpath();

        var nLength = this._driver.YWriteString( mac, this.macPath[ 0 ] + 1,  this.keyUserName, this.keyPassWord, getDriverpath );

        if( this._driver.LastError != 0 ) return null;

        this._driver.SetBuf( nLength, 0 );

        var ret1 = this._driver.YWrite( this.macPath[ 0 ], 1,  this.keyUserName, this.keyPassWord, getDriverpath );

        if( ret1 != 0 ) return null;

        return true;
    },

    /** 获取设备存储用户名*/
    getKeyUserName : function()
    {
        var result = this._getCachedata( this.userNamePath );

        return result || null;
    },

    /** 获取设备存储mac地址*/
    getKeyMac : function()
    {
        var macArray = [];

        for( var i = 0, mac; mac = this.macPath[ i ]; i += 1 )
        {
            var result = this._getCachedata( mac );

            result && macArray.push( result );
        }

        return macArray;
    },

    /** 获取本机MAC地址*/
    getLocalMac : function()
    {
        return this._driver.MacAddr;
    },

    /** 获取设备路径 */
    getDriverpath : function()
    {
        this._findPort = this._driver.FindPort(0);

        return this._findPort;
    },

    /** 是否插入key*/
    isKeyPnp : function( callback )
    {
        var self = this;

        setInterval( function()
        {
            self.getDriverpath();

            self._isKey = self._driver.LastError == -92 ? false : true;

            if( !( self._prevKey == self._isKey ) )
            {
                typeof callback && callback.call( self, self._isKey );
            }

            self._prevKey = self._isKey;

        }, 1000 );
    },

    /** 判断是否安装驱动 */
    isInstallDriver  : function()
    {
        return this._driver.FindPort ? true : false;
    }
};